package com.loong.dilib.support;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * HTTP处理工具
 */
public class HttpTool {

	/** 编码 */
	private static String CHARSET = "UTF-8";
	/** 默认头部信息 */
	public static final String CONTENT_DEFAUL = "application/x-www-form-urlencoded";
	/** Json方式头部信息 */
	public static final String CONTENT_JSON = "application/json";
	/** URL地址参数分隔符 */
	public static final String URL_PARAM_SPLIT = "?";
	/** URL地址分隔符 */
	public static final String URL_SPLIT = "/";
	/** URL参数值分隔符 */
	public static final String PARAM_VALUE_SPLIT = "=";
	/** URL参数分隔符 */
	public static final String PARAMS_SPLIT = "&";

	/** 头部信息名称 */
	private static final String HEADER_NAME = "Content-Type";

	private static CloseableHttpClient httpClient = HttpClients.createDefault();

	/**
	 * Post方式获取HTTP信息，默认方式
	 * 
	 * @param url 访问地址
	 * @param param 访问参数
	 * @return HTTP信息字符串
	 * @throws IOException IO异常
	 */
	public static String getHttpByPost(String url, String param) throws IOException {

		return getHttpByPost(url, param, CONTENT_DEFAUL);
	}

	/**
	 * Post方式获取HTTP信息
	 * 
	 * @param url 访问地址
	 * @param param 访问参数
	 * @param content 访问方式
	 * @return HTTP信息字符串
	 * @throws IOException IO异常
	 */
	public static String getHttpByPost(String url, String param, String content) throws IOException {

		HttpPost post = new HttpPost(url);
		StringEntity entity = new StringEntity(param, CHARSET);
		post.setEntity(entity);
		post.addHeader(HEADER_NAME, content);
		return getHttpEntity(post);
	}

	/**
	 * Get方式获取HTTP信息
	 * 
	 * @param url 访问地址
	 * @param param 访问参数
	 * @return HTTP信息字符串
	 * @throws IOException IO异常
	 */
	public static String getHttpByGet(String url, String param) throws IOException {

		if (param != null) {
			url = url + URL_PARAM_SPLIT + param;
		}
		HttpGet get = new HttpGet(url);
		return getHttpEntity(get);
	}

	/**
	 * 获取相应信息核心处理方法
	 * 
	 * @param request 请求对象
	 * @return HTTP信息字符串
	 * @throws IOException
	 */
	private static String getHttpEntity(HttpUriRequest request) throws IOException {

		String httpStr = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity httpEntity = response.getEntity();
				httpStr = EntityUtils.toString(httpEntity, CHARSET);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
				}
			}
		}
		return httpStr;
	}
}
